# Copyright 2021 Josh Newans
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import rclpy
from rclpy.node import Node

from geometry_msgs.msg import Twist
from geometry_msgs.msg import TwistStamped
from geometry_msgs.msg import Pose
from geometry_msgs.msg import PoseStamped
from rclpy.qos import QoSProfile, ReliabilityPolicy

class TwistUnstamper(Node):

    def __init__(self):
        super().__init__('poseandtwist_unstamper')#节点名称

        self.publisher_ = self.create_publisher(Pose, 'pose', 10)#
        self.publisher1_ = self.create_publisher(Twist, 'twist', 10)
        # 设置为不可靠（BEST_EFFORT）的QoS策略
        qos_profile = QoSProfile(
            depth=10,
            reliability=ReliabilityPolicy.BEST_EFFORT  # 设置为不可靠性
        )
        # 3-1.创建订阅方；
        """
        参数：
            1.消息类型
            2.话题名称
            3.回调函数 
            4.QOS（队列长度）
        返回值：订阅对象
        """
        self.subscription = self.create_subscription(
            PoseStamped,
            'ap/pose/filtered',
            self.listener_callback,
            qos_profile  # 使用不可靠QoS配置
            # 10
            )
        self.subscription = self.create_subscription(
            TwistStamped,
            'ap/twist/filtered',
            self.listener_callback1,
            qos_profile  # 使用不可靠QoS配置
            # 10
            )
        self.subscription  # prevent unused variable warning
        self.get_logger().info("等待posestamped and twiststamped...") 

    def listener_callback(self, inMsg):

        self.publisher_.publish(inMsg.pose)
        # self.get_logger().info('X: "%f"' % outMsg.twist.linear.x)
    def listener_callback1(self, inMsg):

        self.publisher1_.publish(inMsg.twist)
        # self.get_logger().info('X: "%f"' % outMsg.twist.linear.x)

def main(args=None):
    rclpy.init(args=args)

    twist_unstamper = TwistUnstamper()
    rclpy.spin(twist_unstamper)

    twist_unstamper.destroy_node()
    rclpy.shutdown()


if __name__ == '__main__':
    main()
